package main

import (
	"encoding/json"
	"fmt"
	"golang.org/x/net/websocket"
	"time"
)

type TestMsg struct {
	Ver   string `json:"ver"`
	CmdId int    `json:"cmdId"`
	IsZip int    `json:"isZip"`
	Body  struct {
		UserId  string `json:"userId"`
		AppId   string `json:"appId"`
		ImToken string `json:"imToken"`
		GroupId string `json:"groupId"`
	} `json:"body"`
}

func main() {
	dial, err := websocket.Dial("wss://im-linkserver-64.csdn.net", "wss", "https://blog.csdn.net/")
	if err != nil {
		panic(err)
		return
	}
	testMsg := &TestMsg{
		Ver:   "1.0",
		CmdId: 2,
		IsZip: 0,
		Body: struct {
			UserId  string `json:"userId"`
			AppId   string `json:"appId"`
			ImToken string `json:"imToken"`
			GroupId string `json:"groupId"`
		}{
			UserId:  "qq_21050249",
			AppId:   "CSDN-PC",
			ImToken: "17025412341628eSJB4MX",
			GroupId: "CSDN-private-MSG",
		},
	}
	testMsgBytes, _ := json.Marshal(testMsg)
	write, err := dial.Write(testMsgBytes)
	if err != nil {
		panic(err)
		return
	}
	dial.Write([]byte("{\"cmdId\":1}"))

	fmt.Println(write)
	go func(ws *websocket.Conn) {
		for {
			var msg = make([]byte, 512)
			m, err := ws.Read(msg)
			if err != nil {
				fmt.Println(err)
				return
			}
			fmt.Println(string(msg[:m]))
			ws.Write([]byte("{\"cmdId\":1}"))
			time.Sleep(time.Second)
		}

	}(dial)

	time.Sleep(time.Hour)

	fmt.Print(dial)

}
